package cn.xsw.controller;


import cn.xsw.common.ResultBody;
import cn.xsw.entity.BusinessCategory;
import cn.xsw.entity.query.BusinessCategoryQuery;
import cn.xsw.service.BusinessCategoryService;
import cn.xsw.utils.BaseQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *  前端控制器
 * @author liangYiHui
 * @since 2021-05-31
 */
@RestController
@RequestMapping("/business-category")
@Api(tags = "业务分类接口")
public class BusinessCategoryController {

    @Resource
    private BusinessCategoryService businessCategoryService;

    @ApiOperation("查询所有")
    @GetMapping("/findAll")
    public ResultBody findAll(BusinessCategoryQuery query){
        return ResultBody.success(businessCategoryService.list(query.wrapper()));
    }

    @ApiOperation("分页条件查询")
    @GetMapping("/findPage")
    public ResultBody findPage(BusinessCategoryQuery query){
        Page page = businessCategoryService.page(query.page(),query.wrapper());
        return ResultBody.success().page(page);
    }

    @ApiOperation("根据id删除")
    @GetMapping("/delById/{id}")
    public ResultBody delById(@PathVariable("id") int id){
        Boolean b = businessCategoryService.removeById(id);
        if(!b) return ResultBody.error("删除失败");
        return ResultBody.success();
    }

    @ApiOperation("添加或更新（对象id为空：添加，对象id不为空：编辑）")
    @PostMapping("/saveOrUpdate")
    public ResultBody saveOrUpdate(@RequestBody BusinessCategory businessCategory){
        Boolean b = businessCategoryService.saveOrUpdate(businessCategory);
        if(!b) return ResultBody.error("添加失败");
        return ResultBody.success();
    }

}

